﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Walson.Form
{
    public partial class FlightInstrument
    {       

        /// <summary>
        /// 罗盘
        /// </summary>
        [ToolboxItem(false)]
        public partial class RectangleCompass : UserControl
        {
            private int _Max = 360;
            private int _Min = 0;
            private const float _gap = 60;

            private float _Heading;
            public double Heading
            {
                get
                {
                    return _Heading;
                }
                set
                {
                    if (value != _Heading)
                    {
                        while (value < 0)
                            value += 360;
                        while (value > 360)
                            value -= 360;
                        _Heading = (float)value;
                        this.Invalidate();
                    }
                }
            }

            public Bitmap Image
            {
                get;
                private set;
            }

            private Pen _WhitePen;

            public RectangleCompass()
            {
                InitializeComponent();
                _WhitePen = new Pen(Brushes.White, 2);
                this.Image = new Bitmap(1, 1);
            }

            protected void OnPaint0(PaintEventArgs e)
            {
                using (Bitmap bmp = new Bitmap(this.Width, this.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        Font font = new Font("Times NewRoman", 10f);

                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        g.Clear(Color.FromArgb(57, 49, 66));

                        PointF[] polygon = new PointF[3];
                        polygon[0] = new PointF(this.Width / 2, 7);
                        polygon[1] = new PointF(this.Width / 2 - 5, 2);
                        polygon[2] = new PointF(this.Width / 2 + 5, 2);

                        g.FillPolygon(Brushes.White, polygon);

                        float offset = _Heading - (int)_Heading;
                        float start = this.Width / 2 - offset * 30f;
                        PointF[] line = new PointF[2];
                        float x;
                        for (int i = 0; i < this.Width / 15; i++)
                        {
                            x = i * 15f;
                            line[0] = new PointF(start + i * 15f, 10);
                            line[1] = new PointF(start + i * 15f, 15);
                            g.DrawLine(_WhitePen, line[0], line[1]);

                            line[0] = new PointF(start - i * 15f, 10);
                            line[1] = new PointF(start - i * 15f, 15);
                            g.DrawLine(_WhitePen, line[0], line[1]);

                            if (i % 2 == 0)
                            {
                                int h;
                                float len;

                                h = (int)Math.Floor(_Heading + i / 2);
                                if (h >= 360)
                                    h -= 360;
                                len = g.MeasureString(h.ToString(), font).Width;
                                g.DrawString(h.ToString(), font, Brushes.White, start + i * 15f - len / 2, 19);

                                h = (int)Math.Floor(_Heading - i / 2);
                                if (h < 0)
                                    h += 360;
                                len = g.MeasureString(h.ToString(), font).Width;
                                g.DrawString(h.ToString(), font, Brushes.White, start - i * 15f - len / 2, 19);
                            }
                        }
                    }
                    this.Image = bmp;
                    e.Graphics.DrawImage(bmp, 0, 0);
                }
                base.OnPaint(e);
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                using (Bitmap bmp = new Bitmap(this.Width, this.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        Font font = new Font("Times NewRoman", 10f);

                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                        g.Clear(Color.FromArgb(57, 49, 66));

                        PointF[] polygon = new PointF[3];
                        polygon[0] = new PointF(this.Width / 2, 7);
                        polygon[1] = new PointF(this.Width / 2 - 5, 2);
                        polygon[2] = new PointF(this.Width / 2 + 5, 2);

                        g.FillPolygon(Brushes.White, polygon);

                        float offset = (float)(_Heading - (int)_Heading);
                        float start = this.Width / 2 - offset * _gap;
                        PointF[] line = new PointF[2];
                        float x;
                        for (int i = 0; i < this.Width / (_gap / 2); i++)
                        {
                            x = i * _gap / 2;
                            line[0] = new PointF(start + i * _gap / 2, 10);
                            line[1] = new PointF(start + i * _gap / 2, 17);
                            g.DrawLine(_WhitePen, line[0], line[1]);

                            line[0] = new PointF(start - i * _gap / 2, 10);
                            line[1] = new PointF(start - i * _gap / 2, 17);
                            g.DrawLine(_WhitePen, line[0], line[1]);

//                             for (int j = 0; j < 5; j++)
//                             {
//                                 line[0] = new PointF(start + i * _gap / 2 + j * _gap / 5, 10);
//                                 line[1] = new PointF(start + i * _gap / 2 + j * _gap / 5, 15);
//                                 g.DrawLine(Pens.WhiteSmoke, line[0], line[1]);
// 
//                                 line[0] = new PointF(start - i * _gap / 2 + j * _gap / 5, 10);
//                                 line[1] = new PointF(start - i * _gap / 2 + j * _gap / 5, 15);
//                                 g.DrawLine(Pens.WhiteSmoke, line[0], line[1]);
//                             }

                            if (i % 2 == 0)
                            {
                                int h;
                                float len;

                                if (_Heading >= 0)
                                    h = (int)Math.Floor(_Heading + i / 2);
                                else
                                    h = (int)Math.Ceiling(_Heading + i / 2);


                                while (h < _Min)
                                    h = _Max - (_Min - h);
                                while (h > _Max)
                                    h = _Min + h - _Max;
                                if (h == _Max)
                                    h = _Min;

                                //                             if (h >= 360)
                                //                                 h -= 360;
                                len = g.MeasureString(h.ToString(), font).Width;
                                g.DrawString(h.ToString(), font, Brushes.White, start + i * _gap / 2 - len / 2, 19);

                                if (_Heading >= 0)
                                    h = (int)Math.Floor(_Heading - i / 2);
                                else
                                    h = (int)Math.Ceiling(_Heading - i / 2);

                                while (h < _Min)
                                    h = _Max - (_Min - h);
                                while (h > _Max)
                                    h = _Min + h - _Max;
                                if (h == _Max)
                                    h = _Min;
                                //                             if (h < 0)
                                //                                 h += 360;
                                len = g.MeasureString(h.ToString(), font).Width;
                                g.DrawString(h.ToString(), font, Brushes.White, start - i * _gap / 2 - len / 2, 19);
                            }
                        }

                        g.ResetTransform();
//                         line = new PointF[3];
//                         line[0] = new PointF(this.Width - 1, 0);
//                         line[1] = new PointF(0, 0);
//                         line[2] = new PointF(0, this.Height);
//                         g.DrawLines(Pens.Black, line);
// 
//                         line[0] = new PointF(0, this.Height - 1);
//                         line[1] = new PointF(this.Width - 1, this.Height - 1);
//                         line[2] = new PointF(this.Width - 1, 0);
//                         g.DrawLines(Pens.WhiteSmoke, line);
                    }
                    e.Graphics.DrawImage(bmp, 0, 0);
                }
                base.OnPaint(e);
            }
        }
    }
}